package com.workflow.action;

import java.util.Set;

import com.base.service.CardTypeService;
import com.common.bean.Flowtemplatebranch;
//import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Applydept;
import com.workflow.bean.Applydeptdetail;
import com.workflow.service.ApplydeptService;
import com.workflow.service.ApplydeptdetailService;

public class ApplydeptdetailManagerAction extends AbstractActionSupport implements Preparable {
	private static final long serialVersionUID = -2283523294520450394L;

	private ApplydeptdetailService applydeptdetailService;
	
	private ApplydeptService applydeptService;
	
	private CardTypeService cardTypeService;
	
	private Applydeptdetail applydeptdetail;
	
	private Applydept applydept;
	
	private String appdetailno;
	
	private PagerModel pm;
	
	private String applyno;
	
	private Set cardsubtypelist;

	public void prepare() throws Exception {
//		cardsubtypelist = cardTypeService.findAllCardsubtype();
//		cardsubtypelist = cardTypeService.findAllCardsubtype(String cardmaintypeno);//根据主卡类型筛选
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddApplydeptdetail() throws Exception {
		applydept = applydeptService.findApplydept(applyno);
		applydeptdetail = new Applydeptdetail();
		applydeptdetail.setApplydept(applydept);
		cardsubtypelist = applydept.getCardmaintype().getCardsubtypes();
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addApplydeptdetail() throws Exception {
		//需保证applyno的存在、库存限额检查
		/*
		 * 部门、子卡类型、字典（状态）筛选库存限额然后比对是否超出
		 * */
//		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		
		applyno = applydeptdetail.getApplydept().getApplyno();
		applydept = applydeptService.findApplydept(applyno);
		 
		if(StringToken.DICT_FLOWBRANCH_YES.equals(applydept.getDatadictByBranch().getName())){
			Set<Flowtemplatebranch> set = applydept.getFlowtemplatedetail().getFlowtemplatebranchs();
			for(Flowtemplatebranch flowtemplatebranch : set) {
				if(StringToken.DICT_FLOWBRANCH_DEF.equals(flowtemplatebranch.getDefaultrun())){//保存默认分支
					applydeptdetail.setFlowtemplatebranch(flowtemplatebranch);
				}
				if("1".equals(flowtemplatebranch.getBranchseq())) {
					if(applydeptdetail.getCardsubtype().equals(flowtemplatebranch.getCardsubtype())) {
						applydeptdetail.setFlowtemplatebranch(flowtemplatebranch);
					}
				}
				
			}
		}
		applydeptdetailService.addApplydeptdetail(applydeptdetail);
		return SUCCESS;
	}
	
	public String findAllApplydeptdetail() throws Exception {
		applydept = applydeptService.findApplydept(applyno);
		pm = applydeptdetailService.findApplydeptdetailForPager(applyno);
		return SUCCESS;
	}
	
	public String findApplydeptdetail() throws Exception {
		applydeptdetail = applydeptdetailService.findApplydeptdetail(appdetailno);
		return SUCCESS;
	}
	
	public String delApplydeptdetail() throws Exception {
		applydeptdetailService.removeApplydeptdetail(appdetailno);
		return SUCCESS;
	}

	public String getApplydeptdetailForUpdate() throws Exception {
		applydeptdetail = applydeptdetailService.findApplydeptdetail(appdetailno);
		return SUCCESS;
	}
	
	public String updateApplydeptdetail() throws Exception {
		applydeptdetailService.updateApplydeptdetail(applydeptdetail);
		return SUCCESS;
	}

	/**
	 * @return the appdetailno
	 */
	public String getAppdetailno() {
		return appdetailno;
	}

	/**
	 * @param appdetailno the appdetailno to set
	 */
	public void setAppdetailno(String appdetailno) {
		this.appdetailno = appdetailno;
	}

	/**
	 * @return the applydept
	 */
	public Applydept getApplydept() {
		return applydept;
	}

	/**
	 * @param applydept the applydept to set
	 */
	public void setApplydept(Applydept applydept) {
		this.applydept = applydept;
	}

	/**
	 * @return the applydeptdetail
	 */
	public Applydeptdetail getApplydeptdetail() {
		return applydeptdetail;
	}

	/**
	 * @param applydeptdetail the applydeptdetail to set
	 */
	public void setApplydeptdetail(Applydeptdetail applydeptdetail) {
		this.applydeptdetail = applydeptdetail;
	}

	/**
	 * @return the applydeptdetailService
	 */
	public ApplydeptdetailService getApplydeptdetailService() {
		return applydeptdetailService;
	}

	/**
	 * @param applydeptdetailService the applydeptdetailService to set
	 */
	public void setApplydeptdetailService(
			ApplydeptdetailService applydeptdetailService) {
		this.applydeptdetailService = applydeptdetailService;
	}

	/**
	 * @return the applydeptService
	 */
	public ApplydeptService getApplydeptService() {
		return applydeptService;
	}

	/**
	 * @param applydeptService the applydeptService to set
	 */
	public void setApplydeptService(ApplydeptService applydeptService) {
		this.applydeptService = applydeptService;
	}

	/**
	 * @return the applyno
	 */
	public String getApplyno() {
		return applyno;
	}

	/**
	 * @param applyno the applyno to set
	 */
	public void setApplyno(String applyno) {
		this.applyno = applyno;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the cardsubtypelist
	 */
	public Set getCardsubtypelist() {
		return cardsubtypelist;
	}

	/**
	 * @param cardsubtypelist the cardsubtypelist to set
	 */
	public void setCardsubtypelist(Set cardsubtypelist) {
		this.cardsubtypelist = cardsubtypelist;
	}

	/**
	 * @return the cardTypeService
	 */
	public CardTypeService getCardTypeService() {
		return cardTypeService;
	}

	/**
	 * @param cardTypeService the cardTypeService to set
	 */
	public void setCardTypeService(CardTypeService cardTypeService) {
		this.cardTypeService = cardTypeService;
	}
	
	
}
